package com.assignwork.service.impl;

import com.assignwork.dto.VoyageShipInfoSubmitDTO;
import com.assignwork.entity.VoyageShipInfo;
import com.assignwork.mapper.VoyageShipInfoMapper;
import com.assignwork.service.ICommonService;
import com.assignwork.service.IVoyageShipInfoService;
import com.assignwork.vo.VoyageShipInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 船舶信息实现类
 */
@Service
@AllArgsConstructor
public class VoyageShipInfoServiceImpl extends BaseServiceImpl<VoyageShipInfoMapper, VoyageShipInfo> implements IVoyageShipInfoService {

    private ICommonService commonService;

    @Override
    public VoyageShipInfoVO getDetailByVoyageId(String voyageId) {
        QueryWrapper<VoyageShipInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VoyageShipInfo::getIsDeleted, 0 )
                .eq(VoyageShipInfo::getVoyageId, Func.toLong(voyageId));

        List<VoyageShipInfo> voyageShipInfos = baseMapper.selectList(queryWrapper);
        if (!Func.isNull(voyageShipInfos) && !voyageShipInfos.isEmpty()) {
            VoyageShipInfo temp = voyageShipInfos.get(0);
            VoyageShipInfoVO res = BeanUtil.copy(temp, VoyageShipInfoVO.class);
            if (!Func.isNull(res.getCreateUser())) {
                res.setCreateUserName(commonService.userName(res.getCreateUser().toString()));
            }
            if (!Func.isNull(res.getUpdateUser())) {
                res.setUpdateUserName(commonService.userName(res.getUpdateUser().toString()));
            }
            return res;
        } else {
            return null;
        }


    }

    @Override
    public Boolean submitVoyageShipInfo(VoyageShipInfoSubmitDTO dto) {
        VoyageShipInfo entity = BeanUtil.copy(dto, VoyageShipInfo.class);
        return super.saveOrUpdate(entity);
    }
}